summaryrefslogtreecommitdiff
path: root/app/[lng]/admin/edp/components/contract-edit-form.tsx
blob: 5a9ec03eba3aad25166bfcbe9b86d24632bee5ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'use client'

import { useState, useEffect } from 'react'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { toast } from 'sonner'
import { updateContract } from '../actions/contract-actions'
import { getContractById } from '../actions/data-actions'
import { ProjectSelector } from './project-selector'
import { VendorSelector } from './vendor-selector'
import { ContractSelector } from './contract-selector'

interface Project {
  id: number
  code: string
  name: string
  type: string
}

interface Vendor {
  id: number
  vendorName: string
  vendorCode: string | null
  status: string
}

interface Contract {
  id: number
  contractNo: string
  contractName: string
  status: string
  projectId: number
  vendorId: number
  projectCode: string | null
  projectName: string | null
  vendorName: string | null
  vendorCode: string | null
}

interface ContractEditFormProps {
  onContractUpdated?: (contract: { id: number; contractNo: string; contractName: string; status: string }) => void
}

export function ContractEditForm({ onContractUpdated }: ContractEditFormProps) {
  const [loading, setLoading] = useState(false)
  const [selectedContract, setSelectedContract] = useState<Contract | undefined>()
  const [selectedProject, setSelectedProject] = useState<Project | undefined>()
  const [selectedVendor, setSelectedVendor] = useState<Vendor | undefined>()
  const [formData, setFormData] = useState({
    contractName: '',
    status: 'TEST'
  })

  // 계약 선택 시 데이터 로드
  useEffect(() => {
    if (selectedContract) {
      loadContractData(selectedContract.id)
    }
  }, [selectedContract])

  const loadContractData = async (contractId: number) => {
    try {
      const result = await getContractById(contractId)
      if (result.success) {
        const contract = result.data
        setFormData({
          contractName: contract.contractName,
          status: contract.status
        })
        
        // 프로젝트 정보 설정
        if (contract.projectId && contract.projectCode) {
          setSelectedProject({
            id: contract.projectId,
            code: contract.projectCode,
            name: contract.projectName || '',
            type: ''
          })
        }
        
        // 벤더 정보 설정
        if (contract.vendorId && contract.vendorName) {
          setSelectedVendor({
            id: contract.vendorId,
            vendorName: contract.vendorName,
            vendorCode: contract.vendorCode,
            status: 'ACTIVE'
          })
        }
      } else {
        toast.error(result.error)
      }
    } catch (error) {
      toast.error('계약 정보를 불러오는 중 오류가 발생했습니다.')
    }
  }

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    
    if (!selectedContract) {
      toast.error('수정할 계약을 선택해주세요.')
      return
    }
    
    if (!selectedProject || !selectedVendor || !formData.contractName.trim()) {
      toast.error('모든 필수 항목을 입력해주세요.')
      return
    }

    setLoading(true)
    try {
      const result = await updateContract(selectedContract.id, {
        projectId: selectedProject.id,
        vendorId: selectedVendor.id,
        contractName: formData.contractName,
        status: formData.status
      })
      
      if (result.success) {
        toast.success(result.message)
        onContractUpdated?.(result.data)
      } else {
        toast.error(result.error)
      }
    } catch (error) {
      toast.error('계약 수정 중 오류가 발생했습니다.')
    } finally {
      setLoading(false)
    }
  }

  const handleContractSelect = (contract: Contract) => {
    setSelectedContract(contract)
    // 폼 초기화
    setSelectedProject(undefined)
    setSelectedVendor(undefined)
    setFormData({
      contractName: '',
      status: 'TEST'
    })
  }

  return (
    <Card>
      <CardHeader>
        <CardTitle>계약 수정</CardTitle>
        <CardDescription>
          기존 계약의 정보를 수정합니다.
        </CardDescription>
      </CardHeader>
      <CardContent>
        <form onSubmit={handleSubmit} className="space-y-4">
          <div>
            <Label>수정할 계약 선택 *</Label>
            <ContractSelector
              selectedContract={selectedContract}
              onContractSelect={handleContractSelect}
              disabled={loading}
            />
          </div>

          {selectedContract && (
            <>
              <div className="bg-muted/50 p-3 rounded-md">
                <div className="text-sm font-medium">선택된 계약</div>
                <div className="text-sm text-muted-foreground">
                  [{selectedContract.contractNo}] {selectedContract.contractName}
                </div>
              </div>

              <div>
                <Label>프로젝트 *</Label>
                <ProjectSelector
                  selectedProject={selectedProject}
                  onProjectSelect={setSelectedProject}
                  disabled={loading}
                />
              </div>

              <div>
                <Label>벤더 *</Label>
                <VendorSelector
                  selectedVendor={selectedVendor}
                  onVendorSelect={setSelectedVendor}
                  disabled={loading}
                />
              </div>

              <div>
                <Label htmlFor="contractName">계약명 *</Label>
                <Input
                  id="contractName"
                  type="text"
                  value={formData.contractName}
                  onChange={(e) => setFormData(prev => ({ ...prev, contractName: e.target.value }))}
                  placeholder="계약명을 입력하세요"
                />
              </div>

              <div>
                <Label htmlFor="status">계약 상태</Label>
                <Select
                  value={formData.status}
                  onValueChange={(value) => setFormData(prev => ({ ...prev, status: value }))}
                >
                  <SelectTrigger>
                    <SelectValue />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="TEST">TEST</SelectItem>
                    <SelectItem value="DRAFT">DRAFT</SelectItem>
                    <SelectItem value="ACTIVE">ACTIVE</SelectItem>
                    <SelectItem value="PENDING">PENDING</SelectItem>
                  </SelectContent>
                </Select>
              </div>

              <Button type="submit" disabled={loading} className="w-full">
                {loading ? '수정 중...' : '계약 수정'}
              </Button>
            </>
          )}
        </form>
      </CardContent>
    </Card>
  )
}